import { APIS } from "../../utils/api";
import { get, post } from "../../utils/request";

// pages/tabledetail/index.js
Page({
    data: {
        id: '',
        shop: '',
        cnNum: ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'],
        info: {},
        showTooltip: false
    },
    onLoad(options) {
        this.setData({
            ...options
        })
    },

    onShow() {
        this.getDetail();
    },

    handleDel() {
        // wx.showToast({
        //   title: '暂时无法删除',
        //   icon: 'none'
        // })
        post(`${APIS.rulesegment}/destroy/${this.data.shop}/${this.data.id}`)
            .then(res => {
                if (res.code === 0) {
                    wx.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                    wx.navigateBack(-1);
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                }
            })

    },
    handleBeforeDel() {
        this.setData({
            showTooltip: true
        })
    },
    handleCancel() {
        this.setData({
            showTooltip: false
        })
    },
    handleEdit() {
        const {id, shop} = this.data;
        wx.navigateTo({
          url: `/pages/tablenew/index?action=modify&id=${id}&shop=${shop}`,
        })
    },
    getDetail() {
        get(`${APIS.rulesegment}/${this.data.shop}/${this.data.id}`)
            .then(res => {
                console.log(res.data)
                if (res.code === 1) {
                    wx.showToast({
                      title: res.message,
                      icon: 'none'
                    })
                    return;
                }
                let rule = res.data;
                let cLen = rule.table.cards.length;
                let bLen = rule.table.balls.length;
                if (cLen) {
                    rule.table.cards[cLen - 1].isLast = true;
                }
                else if (bLen) {
                    rule.table.balls[bLen - 1].isLast = true;
                }
                this.setData({
                    info: res.data
                })
            })
    }
})